/**
 * Copyright 2007-2010 非也
 * All rights reserved. 
 * 
 * This library is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License v3 as published by the Free Software
 * Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with this library; if not, see http://www.gnu.org/licenses/lgpl.html.
 *
 */
package org.fireflow2.designer.eclipse.commands.service;

import org.eclipse.gef.commands.Command;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.swt.widgets.Shell;
import org.fireflow.model.binding.ServiceBinding;
import org.fireflow2.designer.eclipse.modelwrapper.ModelWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.NodeWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.SubProcessWrapper;
import org.fireflow2.designer.eclipse.modelwrapper.basic.ServiceDefWrapper;

/**
 *
 * @author 非也 nychen2000@163.com
 * Fire Workflow 官方网站：www.firesoa.com 或者 www.fireflow.org
 *
 */
public abstract class AbstractServiceCreationCommand  extends Command {
	protected Shell shell = null;
	protected ServiceBinding serviceBinding = null;
	protected boolean isCanceled = false;
	/**
	 * ParentWrapper只可能是SubProcessWrapper，或者GroupWrapper或者LaneWrapper
	 */
	protected ModelWrapper parentWrapper = null;
	protected ServiceDefWrapper svcDefWrapper = null;
	
	public void setShell(Shell shell){
		this.shell = shell;
	}

	public ServiceBinding getServiceBinding(){
		return serviceBinding;
	}
	
	public boolean isCanceled(){
		return this.isCanceled;
	}
	
	public void setParentWrapper(ModelWrapper parentWrapper){
		this.parentWrapper = parentWrapper;
	}
	
	
	public ModelWrapper getParentWrapper(){
		return this.parentWrapper;
	}
	
	public ServiceDefWrapper getServiceDefWrapper(){
		return this.svcDefWrapper;
	}
	public void execute(){

		//1、打开Java Select界面
		AbstractServiceCreationWizard wizard = createWizard();
		WizardDialog wizardDialog = new WizardDialog(shell,wizard);
		if (parentWrapper instanceof SubProcessWrapper){
			wizard.setSubProcessWrapper((SubProcessWrapper)parentWrapper);
		}else if (parentWrapper instanceof NodeWrapper){
			wizard.setSubProcessWrapper(((NodeWrapper)parentWrapper).getSubProcessWrapper());
		}
		int returnCode = wizardDialog.open();
		if (returnCode==Dialog.OK){
			svcDefWrapper =  wizard.getServiceDefWrapper();
			serviceBinding = wizard.getServiceBinding();
			
			parentWrapper.addChild(svcDefWrapper);
		}else {
			isCanceled = true;
		}
	}
	public void redo(){
		if (!isCanceled){
			parentWrapper.addChild(svcDefWrapper);
		}
	}
	
	public void undo(){
		if (!isCanceled){
			parentWrapper.deleteChild(svcDefWrapper);
		}
	}
	
	protected abstract AbstractServiceCreationWizard createWizard();
}
